package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
        //添加t_checkGroup表
        checkGroupDao.add(checkGroup);
        Integer checkgroup_Id = checkGroup.getId();//获取组id

        //添加t_checkgorup_checkitem表
        if (checkitemIds != null && checkitemIds.length > 0){//Integer[] checkitemIds数组不为空,有勾选,向中间表添加数据
            for (Integer checkitem_id : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkgroup_Id);
                map.put("checkitem_id",checkitem_id);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //去掉前后空格
        if(queryString != null && queryString.length() >0){
            queryString = queryString.trim();
        }

        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 通过ID查询checkGroup
     * @param id
     * @return
     */
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }


    public List<Integer> findCheckItemByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemByCheckGroupId(id);
    }

    @Override
    public void edit(Integer[] checkItemIds, CheckGroup checkGroup) {
        //修改两个表单1)t_checkGroup ; 2)t_checkGroup_checkItem
        //1)t_checkGroup
        checkGroupDao.update(checkGroup);
        //2)t_checkGroup_checkItem
        //2.1)删除checkGroup id 对应的所有联系
        checkGroupDao.deleteByCheckGroupId(checkGroup.getId());
        //2.2)新增checkGroup_id和checkItem_id进t_checkGroup_checkItem表
        if(checkItemIds != null && checkItemIds.length > 0){
            for (Integer checkitemId : checkItemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroup.getId());
                map.put("checkitem_id",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    @Override
    public void delete(Integer id) {
        //判断当前[检查组]是否和[套餐]有所关联
        //1)有关联,则抛出运行异常,[存在与套餐的关联,无法删除]
        long count = checkGroupDao.findCountBySetmeal(id);
        if (count > 0){
            throw new RuntimeException("存在与套餐的关联,无法删除");
        }else {
            //2)无关联
            //2.1)通过id删除t_checkGroup_checkItem下对应的条目
            checkGroupDao.deleteByCheckGroupId(id);
            //2.2)通过id删除t_checkGroup下对应的条目
            checkGroupDao.deleteById(id);
        }
    }
}
